;(function($){
	$.extend($.jgrid,{
		/**
		 * 转换成原始数据类型(数据将提交至后台前需要将 数据的日期字符数据转日期对象)
		 * 支持 grid 数据 的子表(one to many)日期字符转对象
		 * 支持 grid 数据 的主表(many to one)日期字符转对象
		 * 根据grid.p.cmOriginalDataType 将对应字段进行转换
		 */
		convertDataType : function(data,dataTypes){
			for(var i in data) {
				//如果data是个数组,那么name是数组的下标转换.(one to many)
				if (!isNaN(i) && parseInt(i)>=0) {
					$.jgrid.convertDataType(data[i],dataTypes);
				}else{
					var name = $.jgrid.jqID(i);
					for(var dtName in dataTypes){
						var originalDataType = dataTypes[name];
						if(dtName === name || dtName == name){
							if(originalDataType == 'date'){
								if(typeof(data[i])=='object'){
									break;
								}
								if(data[i].replace(" ","").length > 1){
									data[i] = new Date(Date.parse(data[i].replace(/-/g, "/")));
								}else{
									delete data[i];
								}
								break;
							}else if($.isPlainObject(originalDataType)){
								$.jgrid.convertDataType(data[i],originalDataType);
							}
						}
					}
				}
			}
			return data;
		},
		/**
		 * 刷新子表
		 */
		reloadSubGrids : function(subGridMaps){
			var grid = this[0];
			if($.isArray(subGridMaps) && subGridMaps.length>0){
				for(var sgm=0;sgm<subGridMaps.length;sgm++){
					var subGrid = $('#'+subGridMaps[sgm].gridId);
					subGrid.jqGrid("reloadGrid");
					//同时重载下级子表
					if($.isArray(subGrid[0].p.subGridMaps) && subGrid[0].p.subGridMaps.length>0){
						$.jgrid.reloadSubGrids(subGrid[0].p.subGridMaps);
					}
				}
			}
		}
	});

	$.jgrid.extend({
		/**
		 * 获取grid要被保存没数据转换的数据,用于表格内编辑 的校验
		 * 备:日期列数据 如果被转换成对象,将进行表格内编辑的校验(只接受日期字符)
		 */
		getSaveRowDatas : function(){
			var grid = this[0],_this = this,editDatas = [];
			
			//如果启用表格内编辑，需要恢复正在编辑的单元格到不可编辑状态
			_this.jqGrid("restoreCellStatus");
			
			if(grid.p.saveDataScope == 'all'){
				editDatas = _this.jqGrid("getAllRowDatas");
			}else if(grid.p.saveDataScope == 'selection'){
				editDatas = _this.jqGrid("getRowDatasBySelect");
				//选择行对话框
				if(editDatas && editDatas.length < 1){
					$.ui.dialog4Prompt($.jgrid.prompt.selectSave);
					return;
				}
			}else{
				editDatas = _this.jqGrid("getRowDatasByModified");
			}
			return editDatas;
		},
		/**
		 * 获取被处理好的数据(保存时调用)
		 * (主要：日期列数据已经转换日期对象)
		 */
		getPrepareRowDatas : function(){
			var grid = this[0],_this = this,editDatas = _this.jqGrid("getSaveRowDatas");
			
			//判断如果有配置子表,那就获取被选中的行数据
			if($.isArray(grid.p.subGridMaps) && grid.p.subGridMaps.length>0 && editDatas.length<1){
				var _selRowData = _this.jqGrid("getCurrentSelectRowData");
				if(_selRowData.rowStatus == Gfa.GlobalConstant.ROWSTATUS_UNCHANGED){
					$.extend(_selRowData,{rowStatus:Gfa.GlobalConstant.ROWSTATUS_MODIFIED});
				}
				editDatas.push(_selRowData);
			}
			
			//1.根据列的 originalDataType 转换数据类型
			_this.jqGrid('convertToOriginalDataType',editDatas);
			
			//2.当前grid 如果有配置subGridMaps
			if($.isArray(grid.p.subGridMaps) && grid.p.subGridMaps.length>0){
				//2.1.取到实体名称与ID名称
				var objectId = grid.p.jsonReader.id;
				var objectProp = $.StringUtils.getEntityName($.parseJSON(grid.p.postData.queryInfo).queryObjectName);
				//2.2.当前选中行数据
				var selectRowId = _this.jqGrid("getCurrentSelectRowId");
				var selectRow = {};
				
				for(var i=0;i<editDatas.length;i++){
					var elm = editDatas[i];
					//2.3.当前grid选中行的数据
					if(selectRowId == elm[objectId]){
						selectRow = elm;
						//2.4.移除当前选中行
						editDatas.splice(i,1);
						break;
					}
				}
				//3.当前选中行不为空
				if(selectRow && !$.isEmptyObject(selectRow)){
					//3.1.收集子表数据
					for(var sgm=0;sgm<grid.p.subGridMaps.length;sgm++){
						var elm = grid.p.subGridMaps[sgm];
						var subGrid = $('#'+elm.gridId),subGridConfig=subGrid[0].p;
						var subDatas = subGrid.jqGrid('getPrepareRowDatas');
						if(subDatas.length > 0){
							for(var sd=0;sd<subDatas.length;sd++){
								var subData = subDatas[sd];
								//3.2.给子表的子表id和主表属性都赋值
								subData[objectId] = selectRow[objectId];
								subData[objectProp] = selectRow;
							}
						}
						//3.3.再将收集的子表数据,赋值给选中行的子表属性
						selectRow[elm.propName] = subDatas;
					}
					//3.4.最后把选中行数据装回到所有要提交的数据里
					editDatas.push(selectRow);
				}
			}
			return editDatas;
		},
		/**
		 * 给Grid的queryInfo追加查询条件(查询区域中的)
		 */
		setQueryInfoFields4Search : function(fields){
			var grid = this[0];
			grid.p.searchFields = fields;
		},
		/**
		 * 控制button
		 */
		refreshButtonStatus : function(){
			var grid = this[0],_this = this;
			var selectIds = _this.jqGrid('getSelectRowIds');
			var modifiedIds = _this.jqGrid('getRowIdsByModified');
			
			var $deleteBtn = $("#"+grid.p.deleteBtnId);
			var $saveBtn = $("#"+grid.p.saveBtnId);
			var $cancelBtn = $("#"+grid.p.cancelBtnId);
			
			//1.选中行是数据大于0
			if(selectIds.length > 0){
				$deleteBtn.attr("disabled",false);
				$cancelBtn.attr("disabled",false);
				//1.1.初始化控件格式化规则
				$.jqFormatter.initFormatRules(grid.p.editFormSelector);
				//1.2.初始化控件验证规则
				$.jqValidator.initValidateRules(grid.p.editFormSelector);
			} else {
				$deleteBtn.attr("disabled",true);
				$cancelBtn.attr("disabled",true);
			}
			//2.选中行的数据 和 修改行的数据 大于0 
			if(selectIds.length > 0 || modifiedIds.length > 0){
				$saveBtn.attr("disabled",false);
			} else {
				$saveBtn.attr("disabled",true);
			}
		},
		/**
		 * 修改grid的实际数据存储
		 */
		setGridDatasStoreById : function(rowId,rowData){
			var grid = this[0],_this = this,gridData = {};
			//2.1.取到ID名称
			var objectId = grid.p.jsonReader.id;
				
			//判断是否存在	
			var existFlag = false;	
			for(var i=0;i<grid.p.gridDatasStore.length;i++){
				gridData = grid.p.gridDatasStore[i];
				if(gridData[objectId] == rowId || gridData[objectId] === rowId){
					$.extend(grid.p.gridDatasStore[i],rowData);
					existFlag = true;
					break;
				}
			}
			//如果不存在就将新数据,加入gridDatasStore
			if(!existFlag){
				grid.p.gridDatasStore.push(rowData);
			}
		},
		
		delGridDatasStoreById:function(rowId){
			var grid = this[0],_this = this,rows = grid.p.gridDatasStore;
			//2.1.取到ID名称
			var objectId = grid.p.jsonReader.id;
			for(var i=0;i<rows.length;i++){
				
				if(rows[i][objectId] === rowId || rows[i][objectId] == rowId){
					
					rows.splice(i,1);
					break;
				}
			}
		},
		/**
		 * 根据ID获取grid的实际数据存储
		 */
		getGridDatasStoreById : function(rowId){
			var grid = this[0],_this = this,gridData = {};
			//2.1.取到ID名称
			var objectId = grid.p.jsonReader.id;
			for(var i=0;i<grid.p.gridDatasStore.length;i++){
				gridData = grid.p.gridDatasStore[i];
				if(gridData[objectId] == rowId || gridData[objectId] === rowId){
					break;
				}
			}
			return gridData;
		}
	});
})(jQuery);